//
//  WebLinkCategoryController.swift
//  Wallpaper
//
//  Created by YEHWANG-iOS on 2024/10/25.
//

import UIKit
import HandyJSON
import SwiftyUserDefaults
import SwiftyJSON
import Kingfisher
import Lantern
import JXSegmentedView
import JXPagingView
/**
 https://static.wetab.link/hitab/wallpaper-cover/natrue.png
 https://static.wetab.link/hitab/wallpaper-cover/structure.png
 https://static.wetab.link/hitab/wallpaper-cover/animal.png
 https://static.wetab.link/hitab/wallpaper-cover/travel.png
 https://static.wetab.link/hitab/wallpaper-cover/food.png
 https://static.wetab.link/hitab/wallpaper-cover/Anime.png
 https://static.wetab.link/hitab/wallpaper-cover/sport.png
 https://static.wetab.link/hitab/wallpaper-cover/technology.png
 https://static.wetab.link/hitab/wallpaper-cover/street.png
     
 https://api.wetab.link/api/wallpaper/list?client=mobile&pageNo=3&tag=nature
     
 https://api.wetab.link/api/wallpaper/video-list?
 */
class WebLinkCategoryController: UIViewController, JXPagingSmoothViewListViewDelegate {
    
    @IBOutlet weak var collectionView: UICollectionView!

    var items = [[String : String]]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        self.collectionView.keyboardDismissMode = .onDrag
        self.collectionView.contentInsetAdjustmentBehavior = .never
        self.collectionView.register(UINib(nibName: "WebLinkCategoryCell", bundle: nil), forCellWithReuseIdentifier: "WebLinkCategoryCell")
        
        items = [["title":"自然", "img":"https://static.wetab.link/hitab/wallpaper-cover/natrue.png", "type":"nature"],
                 ["title":"建筑", "img":"https://static.wetab.link/hitab/wallpaper-cover/structure.png", "type":"architecture"],
                 ["title":"动物", "img":"https://static.wetab.link/hitab/wallpaper-cover/animal.png", "type":"animals"],
                 ["title":"旅行", "img":"https://static.wetab.link/hitab/wallpaper-cover/travel.png", "type":"travel"],
                 ["title":"美食", "img":"https://static.wetab.link/hitab/wallpaper-cover/food.png", "type":"food-drink"],
                 ["title":"动漫", "img":"https://static.wetab.link/hitab/wallpaper-cover/Anime.png", "type":"anime"],
                 ["title":"运动", "img":"https://static.wetab.link/hitab/wallpaper-cover/sport.png", "type":"athletics"],
                 ["title":"技术", "img":"https://static.wetab.link/hitab/wallpaper-cover/technology.png", "type":"technology"],
                 ["title":"街头", "img":"https://static.wetab.link/hitab/wallpaper-cover/street.png", "type":"street-photography"]]
        
    }
    
    func bottomHeight() -> CGFloat {
        var hasLiuHai: Bool = false
        let window = UIApplication.shared.windows.first
        hasLiuHai = window?.safeAreaInsets.bottom ?? 0 > 0
        return hasLiuHai ? 34.0 : 0.0
    }
    
    deinit {
        
    }
}

extension WebLinkCategoryController: UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WebLinkCategoryCell", for: indexPath) as? WebLinkCategoryCell {
            let item = items[indexPath.item]
            cell.icon.setImage(fromURL: item["img"], loadingStyle: .noani)
            cell.name.text = item["title"]
            return cell
        }
        return UICollectionViewCell()
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let item = items[indexPath.item]
        let vc = WebLinkListController.fromStoryboard(.main)
        vc.tag = item["type"]!
        vc.navigationItem.title = item["title"]!
        //JXPagingSmoothView有问题，self.navigationController拿不到
        topVC?.navigationController?.pushViewController(vc, animated: true)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = (collectionView.frame.width-2)/2.0
        let imageSize = CGSize(width: 200, height: 120)
        let scale = imageSize.height * 1.0 /  imageSize.width
        return CGSize(width: width, height: width * scale)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 2
    }
    
}


extension WebLinkCategoryController: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }

    func listScrollView() -> UIScrollView {
        return collectionView
    }

    func listDidAppear() {
//        if refreshControl?.isRefreshing == true {
//            refreshControl?.beginRefreshing()
//        }
    }

    func listDidDisappear() {

    }
}
